package cn.edu.nuc.weiboclient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.nuc.modle.*;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class WeiBoItemAdapter extends BaseAdapter{
	private Context context = null;
	private List<WeiBoData> weiBoDatas = null;
	private Map<Integer,View> viewMaps = new HashMap<Integer,View>();
	private AsyncImageLoader loader = new AsyncImageLoader();
	public WeiBoItemAdapter(Context context,List<WeiBoData> weiBoDatas){
		this.context = context;
		this.weiBoDatas = weiBoDatas;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = viewMaps.get(position);
		if(rowView == null){
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			rowView = inflater.inflate(R.layout.mainitem, null);
			WeiBoData weiBoData = (WeiBoData)getItem(position);
			TextView nickTextView = (TextView)rowView.findViewById(R.id.mainNickId);
			TextView timeTextView = (TextView)rowView.findViewById(R.id.mainDateId);
			TextView contextTextView = (TextView)rowView.findViewById(R.id.mainTextId);
			final ImageView weiBoImageView = (ImageView)rowView.findViewById(R.id.picId);
			final ImageView headImageView = (ImageView)rowView.findViewById(R.id.headImageId);
			TextView forwardTextView = (TextView) rowView.findViewById(R.id.mainForwardTextId);
			TextView fromTextView = (TextView)rowView.findViewById(R.id.mainFromId);
			System.out.println(weiBoData.getSource());
			fromTextView.setText("from: " + weiBoData.getFrom());
			if(weiBoData.getSource()!=null){
				forwardTextView.setText("@"+weiBoData.getSource().getNick()+":"+weiBoData.getSource().getOrigtext());
			}

			nickTextView.setText(weiBoData.getNick());
			contextTextView.setText(weiBoData.getText());
			String date  = new java.text.SimpleDateFormat
			("HH:mm").format(new java.util.Date(weiBoData.getTimestamp()*1000)) ;
			timeTextView.setText(date);
		
			System.out.println("timestamp---->" + weiBoData.getTimestamp());
			if(!weiBoData.getHead().equals("")){
				Drawable headDrawable = loader.loadDrawable(weiBoData.getHead() + "/50", new AsyncImageLoader.ImageCallback() {
				
					@Override
					public void imageLoaded(Drawable imageDrawable) {
						headImageView.setImageDrawable(imageDrawable);
					}
				});
				if(headDrawable != null){
					headImageView.setImageDrawable(headDrawable);
				}
			}
			else{
				headImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.icon));
			}
			if(weiBoData.getImage() != null){
				Drawable weiBoImageDrawable = loader.loadDrawable(weiBoData.getImage().get(0) + "/460", new AsyncImageLoader.ImageCallback() {
					
					@Override
					public void imageLoaded(Drawable imageDrawable) {
						weiBoImageView.setImageDrawable(imageDrawable);
					}
				});
				if(weiBoImageDrawable != null){
					weiBoImageView.setImageDrawable(weiBoImageDrawable);
				}
			}
			viewMaps.put(position, rowView);
		}
		return rowView;
	}
	@Override
	public int getCount() {
		return weiBoDatas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return weiBoDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


}
